/**
 * Copyright (C) 2008 Motudo Studio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package motudo.screen;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

/**
 * @author game@motudo.com (motudo)
 *
 */
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
	
    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    public void preWindowOpen() {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize(new Point(20, 50));
        configurer.setShowCoolBar(false);
        configurer.setShowStatusLine(false);
        configurer.setShowMenuBar(false); // remove menu bar
        
        // only work in the create time 
        configurer.setShellStyle(SWT.ON_TOP | SWT.CLOSE | SWT.TITLE);
        configurer.setTitle("Motudo");
    }
    
    @Override
    public void postWindowOpen() {
    	super.postWindowOpen();
    	//Shell shell = this.getWindowConfigurer().getWindow().getShell();
    	//shell.setFullScreen(true);
    	this.timerFullScreen();
    }
    
    public void timerFullScreen(){
    	
    	final IWorkbench workbench = PlatformUI.getWorkbench();
    	
    	final IWorkbenchWindow window = this.getWindowConfigurer().getWindow();
    	final Shell shell = window.getShell();
    	
    	
    	if (workbench == null)
			return;
		final Display display = workbench.getDisplay();
		
		display.timerExec(Application.TIME_WORK, new Runnable(){

			@Override
			public void run() {
				shell.setFullScreen(!shell.getFullScreen());
				shell.forceActive(); // always on top
				if(shell.getFullScreen()){
					display.timerExec(Application.TIME_BLOCK, this);					
				} else {					
					display.timerExec(Application.TIME_WORK, this);					
				}
			}});
    }
}
